package castle.defense.project;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

/**
 * The thread that runs the menu screen.
 * 
 * @author Daniel Wasserman
 *
 */
public class MenuThread extends Thread {
	private SurfaceHolder sholder;
	private int backGroundColor = Color.BLACK;
	private ArrayList<SoftButton> buttons;
	private boolean running, paused, started;
	private Context context;

	/**
	 * @param c The context this thread is in.
	 * 
	 * The main thread to run the Menu Screen.
	 */
	public MenuThread(Context c) {
		this.context = c;
		started = false;
		paused = true;
		running = false;
	}

	/**
	 * Initialize all the buttons on the menu screen.
	 * NOTE: Must be called AFTER the screen is initialized (requires screen to meter translation).
	 */
	private void initializeButtons() {
		buttons = new ArrayList<SoftButton>();
		buttons.add(new SoftButton(PE.regionByPercent(25, 80, 50, 10)));
		buttons.get(0).setRunnable(new Runnable() {
			public void run() {
				Intent startGame = new Intent(MenuThread.this.context, GameActivity.class);
				MenuThread.this.context.startActivity(startGame);
			}
		});
		buttons.get(0).setTextOne("Start");

		buttons.add(new SoftButton(PE.regionByPercent(25, 55, 50, 10)));
		buttons.get(1).setTextOne("Options");
		buttons.get(1).setRunnable(new Runnable() {
			public void run() {
				CastleDefenseActivity.internal=true;
				Intent openOptions = new Intent(MenuThread.this.context, Options.class);
				MenuThread.this.context.startActivity(openOptions);
			}
		});

		buttons.add(new SoftButton(PE.regionByPercent(25, 30, 50, 10)));
		buttons.get(2).setTextOne("Highscores");
		buttons.get(2).setRunnable(new Runnable() {
			public void run() {
				CastleDefenseActivity.internal=true;
				Intent openHighscoreScreen = new Intent(MenuThread.this.context, Highscore.class);
				MenuThread.this.context.startActivity(openHighscoreScreen);
			}
		});
	}

	/**
	 * @param e MotionEvent for the "down" event.
	 */
	public void onDown(MotionEvent e) {
		if(buttons!=null)
			for(SoftButton b : buttons) {
				b.onTouch(e);
			}
	}

	/**
	 * 
	 * @param h The surface holder for the thread
	 * 
	 * The surface holder allows the program to get the canvas to draw things.
	 */
	public void setSurfaceHolder(SurfaceHolder h) {
		this.sholder = h;
	}
	/**
	 * @param r Set whether the program should start running or stop.
	 */
	public void setRunning(boolean r) {running = r;}
	/**
	 * @param p Whether or not to pause the thread
	 */
	public void setPaused(boolean p) {paused = p;}

	/**
	 * Function to start the thread running.
	 */
	public void start()
	{
		if(!started)
		{
			started = true;
			setPaused(false);
			setRunning(true);
			initializeButtons();
			super.start();
		}
	}
	/**
	 * Pretty self explanatory.
	 */
	public void run() {
		while(running)
		{
			if(!paused)
			{
				long curTime = System.currentTimeMillis();
				Canvas c = null;
				try {
					c = sholder.lockCanvas();
					synchronized(sholder) {
						doDraw(c);
					}
				} finally {
					if(c!=null)
						sholder.unlockCanvasAndPost(c);
				}
			}
			else
			{

			}
		}
	}
	/**
	 * @param c The canvas to be drawn on.
	 * 
	 * This function tells all objects in the menu to draw themselves on the screen.
	 */
	public void doDraw(Canvas c) {
		GraphicsEngine.drawBackground(c);
		//		c.drawColor(this.backGroundColor);
		for(SoftButton b : buttons) {
			b.draw(c);
		}
	}
}
